﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Data;

/// <summary>
/// Summary description for dbSanPham
/// </summary>
public class dbSanPham
{
    public void AddNewSanPham(SanPham sp, string xml, int ID)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xml);
        //
        XmlNode root = xmlDoc.DocumentElement;
        //
        XmlElement LoaiSP = (XmlElement)root.SelectSingleNode("LoaiSP[@id='" + ID.ToString() + "']");
        XmlElement yamaha = xmlDoc.CreateElement("yamaha");
        int masp = 1;
        int sl = LoaiSP.ChildNodes.Count;
        foreach (int i in yamaha)
        {
            if (Convert.ToInt32(yamaha.GetElementsByTagName("MaSP")) != i)
            {
                masp = i;
            }
        }
        masp = sl + 1;
        XmlElement MaSP = xmlDoc.CreateElement("MaSP");
        XmlElement TenSP = xmlDoc.CreateElement("TenSP");
        XmlElement DacTinh = xmlDoc.CreateElement("DacTinh");
        //
        XmlElement KyThuat = xmlDoc.CreateElement("KyThuat");
        //
                XmlElement LoaiDongCo = xmlDoc.CreateElement("LoaiDongCo");
                XmlElement DungTich = xmlDoc.CreateElement("DungTich");
                XmlElement HTDanhLua = xmlDoc.CreateElement("HeThongDanhLua");
        //
                XmlElement LoaiKhung = xmlDoc.CreateElement("LoaiKhung");
                XmlElement KichThuocKX = xmlDoc.CreateElement("KichThuoc");
        //
                XmlElement KichThuocDR = xmlDoc.CreateElement("KichThuocDaiRong");
                XmlElement DoCao = xmlDoc.CreateElement("DoCao");
        //
                XmlElement ThoiGian = xmlDoc.CreateElement("ThoiGian");
        XmlElement GiaTien = xmlDoc.CreateElement("GiaTien");
        XmlElement Hinh = xmlDoc.CreateElement("Hinh");
        //
        KyThuat.SetAttribute("MaSP", masp.ToString());
        //
        MaSP.InnerText = masp.ToString();
        TenSP.InnerText = sp.TenSanPham;
        DacTinh.InnerText = sp.DacTinh;
        //
        LoaiDongCo.InnerText = sp.LoaiDongCo;
        DungTich.InnerText = sp.DungTich;
        HTDanhLua.InnerText = sp.HTDanhLua;
        //
        LoaiKhung.InnerText = sp.LoaiKhung;
        KichThuocKX.InnerText = sp.KichThuoc;
        //
        KichThuocDR.InnerText = sp.KichThuocDR;
        DoCao.InnerText = sp.DoCao;
        //
        ThoiGian.InnerText = sp.ThoiGianBH;
        //
        GiaTien.InnerText = sp.GiaTien.ToString();
        Hinh.InnerText = sp.Hinh;
        //
        LoaiSP.AppendChild(yamaha);
        //
        yamaha.AppendChild(MaSP);
        yamaha.AppendChild(TenSP);
        yamaha.AppendChild(DacTinh);
        yamaha.AppendChild(KyThuat);
        //
        KyThuat.AppendChild(LoaiDongCo);
        KyThuat.AppendChild(DungTich);
        KyThuat.AppendChild(HTDanhLua);
        KyThuat.AppendChild(LoaiKhung);
        KyThuat.AppendChild(KichThuocKX);
        KyThuat.AppendChild(KichThuocDR);
        KyThuat.AppendChild(DoCao);
        KyThuat.AppendChild(ThoiGian);
        //
        yamaha.AppendChild(GiaTien);
        yamaha.AppendChild(Hinh);
        xmlDoc.AppendChild(root);
        //
        xmlDoc.Save(xml);
    }
    public void DeleteSanPham(int id, int masp, string xml)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xml);
        XmlElement root = xmlDoc.DocumentElement;
        XmlElement LoaiSP = (XmlElement)root.SelectSingleNode("LoaiSP[@id='" + id + "']");
        XmlElement MaSP = (XmlElement)LoaiSP.SelectSingleNode("yamaha[MaSP='" + masp + "']");
        int sl = LoaiSP.ChildNodes.Count;
        LoaiSP.RemoveChild(MaSP);
        if (sl > 2)
        {
            for (int i = masp + 1; i <= sl; i++)
            {
                XmlNode LoaiSPNode = root.SelectSingleNode("LoaiSP[@id='" + id.ToString() + "']");
                XmlNode yamahaNode = LoaiSPNode.SelectSingleNode("yamaha[MaSP='" + i + "']");
                XmlNode oldMaSP = yamahaNode.SelectSingleNode("MaSP");
                oldMaSP.InnerText = masp.ToString();
                xmlDoc.Save(xml);
                masp++;
            }
        }
        xmlDoc.Save(xml);
    }
    public void UpdateSanPham(SanPham sp, int id, string xml,int masp)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xml);
        XmlNode root = xmlDoc.DocumentElement;
        XmlNode LoaiSP = root.SelectSingleNode("LoaiSP[@id='" + id.ToString() + "']");
        XmlNode yamaha = LoaiSP.SelectSingleNode("yamaha[MaSP='" + masp + "']");
        XmlNode oldTenSP = yamaha.SelectSingleNode("TenSP");
        oldTenSP.InnerText = sp.TenSanPham;
        XmlNode oldDacTinh = yamaha.SelectSingleNode("DacTinh");
        oldDacTinh.InnerText = sp.DacTinh;
        XmlNode oldLoaiDongCo = yamaha.SelectSingleNode("KyThuat/LoaiDongCo");
        oldLoaiDongCo.InnerText = sp.LoaiDongCo;
        XmlNode oldDungTich = yamaha.SelectSingleNode("KyThuat/DungTich");
        oldDungTich.InnerText = sp.DungTich;
        XmlNode oldHTDanhLua = yamaha.SelectSingleNode("KyThuat/HeThongDanhLua");
        oldHTDanhLua.InnerText = sp.HTDanhLua;
        XmlNode oldLoaiKhung = yamaha.SelectSingleNode("KyThuat/LoaiKhung");
        oldLoaiKhung.InnerText = sp.LoaiKhung;
        XmlNode oldKichThuoc = yamaha.SelectSingleNode("KyThuat/KichThuoc");
        oldKichThuoc.InnerText = sp.KichThuoc;
        XmlNode oldKichThuocDR = yamaha.SelectSingleNode("KyThuat/KichThuocDaiRong");
        oldKichThuocDR.InnerText = sp.KichThuocDR;
        XmlNode oldDoCao = yamaha.SelectSingleNode("KyThuat/DoCao");
        oldDoCao.InnerText = sp.DoCao;
        XmlNode oldThoiGianBH = yamaha.SelectSingleNode("KyThuat/ThoiGian");
        oldThoiGianBH.InnerText = sp.ThoiGianBH;
        XmlNode oldGiaTien = yamaha.SelectSingleNode("GiaTien");
        oldGiaTien.InnerText = sp.GiaTien.ToString();
        XmlNode oldHinh = yamaha.SelectSingleNode("Hinh");
        oldHinh.InnerText = sp.Hinh;
        xmlDoc.Save(xml);
    }
}